package com.cf.data.serve.environment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;

@Slf4j
public class TCPServer_db {

    public static void db() throws IOException {
        String[] sb = new String[10];
        // 获取今天0点
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);

        // 获取今天的59分59秒
        LocalTime endOfDay = LocalTime.of(23, 59, 59);
        for(int i=0;i<=sb.length;i++){
            String formData = "beginTime="+startOfDay+"&endTime="+endOfDay+"&sbbh="+sb[i];
            // 创建URL对象
            URL url = new URL("http://gdsmzkqxt.weiyouyuan.com.cn/api/db/list");
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");
            // 设置请求属性，表示是表单数据
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 设置允许输入输出
            connection.setDoOutput(true);

            // 发送请求参数
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = formData.getBytes("UTF-8");
                os.write(input, 0, input.length);
            }

            // 获取响应码
/*        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);*/
            Map<String,Object> map;
            // 读取响应
            try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) {
                String responseLine;
                String json = "";
                while ((responseLine = br.readLine()) != null) {
                    //System.out.println(responseLine);
                    json = responseLine;
                }
                JSONObject jsonObject = JSON.parseObject(json);
                map = jsonObject.getInnerMap();
                System.out.println(map);

            }

            // 关闭连接
            connection.disconnect();
            // 目标URL
            // 表单数据，格式为key1=value1&key2=value2
        }

    }
}
